<template>
	<view class="detail_wrap">
		<view class="video_item">
			<video id="myVideo" autoplay="true" :src="ossUrl + videoUrl"></video>
		</view>
		<view class="flex-row justify-between items-center group_3">
			<text class="font row_1" style="width: 86%;">{{ detail.title }}</text>
			<view class="flex-col justify-start items-center badge text-wrapper" v-if="detail.user_star == 0" @click="bindCollect(detail.user_star)"><text class="font_2 text_2">收藏</text>
			</view>
			<view class="flex-col justify-start items-center badge text-wrappers" v-if="detail.user_star == 1" @click="bindCollect(detail.user_star)"><text class="font_2">已收藏</text>
			</view>
		</view>
		<view class="flex-row justify-between items-baseline group_4">
			<text class="font_2 text_3">观看人数：{{ detail.watch_number }}  <text style="margin-left: 30rpx;color: #00000050;">{{ detail.createtime }}</text> </text>
			<text class="text_5">{{ detail.price }}元</text>
		</view>
		<view class="chapter_wrap">
			<view class="chapter_nav">
				<view :class="nav_index == 0 ? 'nav_item active' : 'nav_item'" @click="toNav(0)">课程目录</view>
				<view :class="nav_index == 1 ? 'nav_item active' : 'nav_item'" @click="toNav(1)">课程介绍</view>
			</view>
			<scroll-view scroll-y="true" style="height: 500rpx;" v-if="nav_index == 0">
				<view class="chapter_item" v-for="(item,index) in chapter">
					<view class="item_cont flex justify-between" @click="toPlay(item.file,detail.user_buy)">
						<view class="title">{{index+1}}、{{ item.title }}</view>
						<view class="right_size" v-if="item.is_free == 1 && detail.user_buy == 0">可试看</view>
						<view class="right_size" style="color: #1C41F150;" v-if="item.is_free == 0 && detail.user_buy == 0">需购买</view>
					</view>
				</view>
			</scroll-view>

			<scroll-view scroll-y="true" style="height: 500rpx;" v-if="nav_index == 1">
				<view class="chapter_item">
					<rich-text :nodes="detail.content"></rich-text>
				</view>
			</scroll-view>
		</view>

		<view class="bot_btn">
			<view style="position: relative;">
				<view class="btn">去咨询</view>
				<button open-type="contact"></button>
			</view>
			<view class="save_buy" @click="toPay()">{{ detail.user_buy == 0 ? '立即购买' : '已购买'}}</view>
		</view>
	</view>
</template>

<script>
	import {
		remoteUrl,
		ossUrl
	} from '@/utils/config.js'
	import {
		getCourseInfo,
		starCourse,
		getCourseContent,
		addOrder,
		courseChapter
	} from '@/api/coures.js'
	export default {
		data() {
			return {
				ossUrl: ossUrl,
				remoteUrl: remoteUrl,
				videoUrl: '',
				detail: '',
				nav_index: 0,
				course_id: '',  // 课程id
				chapter: [],
			};
		},
		onLoad(options) {
			this.course_id = options.id
		},
		onShow() {
			this.getCourseInfoFun()
		},
		onShareAppMessage(res) { //发送给朋友
			return {
				title: '小程序分享',
				imageUrl: '',
			}
		},
		methods: {
			// 获取课程详情
			getCourseInfoFun() {
				getCourseInfo({
					id: this.course_id
				}).then(res => {
					if (res.code == 1) {
						this.detail = res.data
						this.getChapter()
					}
				})
			},
			// 获取课程章节
			getChapter() {
				courseChapter({
					course_id: this.course_id
				}).then(res => {
					if (res.code == 1) {
						let index = res.data.findIndex(item => item.is_free == 1)
						if (this.detail.user_buy == 1) {
							index = 0
						}
						this.chapter = res.data
						this.videoUrl = index >= 0 ? res.data[index].file : ''
					}
				})
			},
			// 播放
			toPlay(url,buy) {
				if (buy == 0) {
					uni.navigateTo({
						url: '../couresOrder/couresOrder?detail=' + JSON.stringify(this.detail)
					})
					return
				}
				this.videoUrl = url
			},
			// 收藏
			bindCollect(flag) {
				starCourse({
					id: this.detail.id
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: flag == 0 ? '收藏成功' : '取消收藏成功',
							icon: 'none'
						})
						this.detail.user_star = (flag == 0 ? 1 : 0)
					}
				})
			},
			toNav(index) {
				this.nav_index = index
			},
			toPay() {
				if (this.detail.user_buy == 1) return
				uni.navigateTo({
					url: '../couresOrder/couresOrder?detail=' + JSON.stringify(this.detail)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.detail_wrap {
		min-height: 100vh;
		background: #F9F9F9;
		padding-bottom: 120rpx;

		.video_item {
			width: 690rpx;
			height: 360rpx;
			margin: 0 auto;
			text-align: center;

			video {
				width: 100%;
				height: 100%;
				margin-top: 30rpx;
				border-radius: 10rpx;
			}
		}

		.group_3 {
			padding: 20rpx 30rpx;
			margin-top: 34rpx;

			.font {
				font-size: 34rpx;
				color: #000000;
			}

			.badge {
				margin-right: 6rpx;

				.font_2 {
					font-size: 22rpx;
				}

				.text_2 {
					color: #1c41f1;
				}
			}

			.text-wrapper {
				padding: 5rpx 16rpx;
				background-color: #ffffff;
				border-radius: 54rpx;
				border: solid 1rpx #1c41f1;
			}
			.text-wrappers {
				padding: 5rpx 16rpx;
				background-color: #1c41f1;
				border-radius: 54rpx;
				color: #fff;
				border: solid 1rpx #1c41f1;
			}
		}

		.group_4 {
			padding: 2rpx 30rpx;

			.font_2 {
				font-size: 22rpx;
			}

			.text_3 {
				color: #000000;
			}

			.text_5 {
				color: #1C41F1;
				font-size: 22rpx;
			}
		}

		.chapter_wrap {
			width: 690rpx;
			min-height: 491rpx;
			margin: 20rpx auto 0;
			max-height: 620rpx;
			background: #F1F1F1;
			border-radius: 10rpx;
			padding: 34rpx 30rpx;

			.chapter_nav {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;

				.nav_item {
					margin-right: 80rpx;
					font-size: 32rpx;
					position: relative;
				}

				.active {
					color: #1c41f1;
					font-weight: bold;
				}

				.active::after {
					content: "";
					width: 56rpx;
					border-bottom: 6rpx solid #1c41f1;
					position: absolute;
					bottom: -15rpx;
					left: 50%;
					transform: translate(-50%);
				}
			}

			.chapter_item {
				font-size: 28rpx;

				.item_cont {
					padding: 28rpx 0;

					.right_size {
						color: #00000050
					}
				}

				::v-deep .uni-collapse {
					background: #F1F1F1 !important;

					.uni-collapse-item {
						border-bottom: solid 1rpx #0000001a;
					}

					.uni-collapse-item__wrap {
						background: #F1F1F1 !important;
					}

					.uni-collapse-item__title-box {
						background: #F1F1F1 !important;
						padding: 0 !important;
					}

					.uni-collapse-item__title-arrow {
						margin-right: 0;
					}
				}
			}
		}

		.bot_btn {
			width: 100vw;
			height: 108rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-around;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 10;

			.btn {
				width: 228rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: #fff;
				border: 1rpx solid #1C41F1;
				text-align: center;
				border-radius: 54rpx;
				margin: 0 auto;
				color: #1C41F1;
				font-size: 28rpx;
			}

			button {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				opacity: 0;
			}
		
			.save_buy {
				width: 402rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: #1C41F1;
				text-align: center;
				border-radius: 54rpx;
				color: #fff;
				font-size: 28rpx;
			}
		}
	}
</style>